//
//  MYVisitorView.swift
//  Sina
//
//  Created by 王亮龙1 on 16/5/20.
//  Copyright © 2016年 lianglong. All rights reserved.
//

@objc protocol MYVisitorViewDelegate {
    
    optional func gotoLoginVC()
    optional func gotoRegistorVC()
}

import UIKit
import SnapKit


class MYVisitorView: UIView {
    
    //设置代理属性, 代理对象要弱引用
    weak var vistorDelegate: MYVisitorViewDelegate?
    
    
    //MARK:构造方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        let value:CGFloat = 237/255.0//遮罩图片的背景颜色值
        backgroundColor = UIColor(red: value, green: value, blue: value, alpha: 1.0)
        
        setUpSubViews()
        
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
    //MARK:根据不同页面的设置,显示不同的访客视图模板
    func upDateUI(title: String, imageName: String, isHome: Bool = false) {
        
        if isHome {
            
            startAnimation()
        } else {
            
            //如果不是首页,把房子隐藏,把圆圈移到最顶层,在后边把 圆圈 的image换成其他界面的image(其实后边的界面, 如消息,我的,发现界面换的不是房子的image,而是圆圈的,因为房子视图和圆圈视图的center一样,所以才...)
            
            mainImage.hidden = true
            bringSubviewToFront(circleIcon)
        }
        circleIcon.image = UIImage(named: imageName)
        tipLabel.text = title
    }
    
    
    //让 circleIcon 转动的方法
    private func startAnimation() {
        
        let basicAnim = CABasicAnimation(keyPath: "transform.rotation")
        
        basicAnim.duration = 15//一次动画的时间
        basicAnim.repeatCount = MAXFLOAT//动画的次数
        basicAnim.toValue = M_PI * 2//转动的角度
        //当视图被销毁,或者处于非活跃状态,动画不从图层中移除
        basicAnim.removedOnCompletion = false
        
        //给圆圈视图添加动画
        circleIcon.layer.addAnimation(basicAnim, forKey: nil)
    }
    
    
    
    
    //MARK:添加子控件
    private func setUpSubViews() {
        
        //添加子控件
        addSubview(circleIcon)
        addSubview(backIcon)
        addSubview(mainImage)
        addSubview(tipLabel)
        addSubview(loginBtn)
        addSubview(registerBtn)
        
        
        //MARK:设置子控件的约束
        circleIcon.snp_makeConstraints { (make) in
            make.centerX.equalTo(self.snp_centerX)
            make.centerY.equalTo(self.snp_centerY).offset(-80)
        }
        
        backIcon.snp_makeConstraints { (make) in
            make.center.equalTo(circleIcon.snp_center)
        }
        
        mainImage.snp_makeConstraints { (make) in
            make.center.equalTo(circleIcon.snp_center)
        }
        
        tipLabel.snp_makeConstraints { (make) in
            make.top.equalTo(circleIcon.snp_bottom).offset(16)
            make.centerX.equalTo(circleIcon.snp_centerX)
            make.height.equalTo(40)
        }
        
        loginBtn.snp_makeConstraints { (make) in
            make.left.equalTo(tipLabel.snp_left)
            make.top.equalTo(tipLabel.snp_bottom).offset(16)
            make.width.equalTo(100)
        }
        
        registerBtn.snp_makeConstraints { (make) in
            make.right.equalTo(tipLabel.snp_right)
            make.top.equalTo(tipLabel.snp_bottom).offset(16)
            make.width.equalTo(100)
        }
        
        
    }
    
    
    
    //MARK:懒加载子控件
    private lazy var circleIcon: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    private lazy var backIcon: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    private lazy var mainImage: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    private lazy var tipLabel: UILabel = {
        
        let tiplabel = UILabel()
        
        tiplabel.text = "关注一些人,回到这里看看有什么惊喜"
        tiplabel.font = UIFont.systemFontOfSize(14)
        tiplabel.textColor = UIColor.darkGrayColor()
        tiplabel.textAlignment = .Center
        tiplabel.numberOfLines = 0
        //最大布局宽度
        tiplabel.preferredMaxLayoutWidth = 230
        
        tiplabel.sizeToFit()
        
        return tiplabel
    }()
    
    private lazy var loginBtn: UIButton = {
        
        let loginBtn = UIButton()
        
        loginBtn.setTitle("登录", forState: .Normal)
        loginBtn.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        loginBtn.titleLabel?.font = UIFont.systemFontOfSize(15.0)
        loginBtn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        loginBtn.addTarget(self, action: #selector(MYVisitorView.loginBtnClick), forControlEvents: .TouchUpInside)
        
        return loginBtn
    }()
    
    private lazy var registerBtn: UIButton = {
        
        let registerBtn = UIButton()
        
        registerBtn.setTitle("注册", forState: .Normal)
        registerBtn.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        registerBtn.titleLabel?.font = UIFont.systemFontOfSize(15.0)
        registerBtn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        registerBtn.addTarget(self, action: #selector(MYVisitorView.registerBtnClick), forControlEvents: .TouchUpInside)
        
        return registerBtn
    }()
    
    
    
    
    //MARK:登录按钮的点击方法
    @objc private func loginBtnClick() {
        
        //问号表示是否响应方法
        vistorDelegate?.gotoLoginVC?()
    }
    
    //MARK:登录按钮的点击方法
    @objc private func registerBtnClick() {
        
        vistorDelegate?.gotoRegistorVC?()
    }

    
    
    
}



